MicroSoft.Owin 自托管+WebApi

您所在的位置:网站首页 webapi microsoft MicroSoft.Owin 自托管+WebApi

MicroSoft.Owin 自托管+WebApi

2022-12-18 09:57| 来源: 网络整理| 查看: 265

版本 .Net Framework 4.6 在Vs2017程序包管理控制台中运行以下命令

安装 Microsoft.Owin.SelfHost Install-Package Microsoft.Owin.SelfHost -Version 4.0.0

安装 Microsoft.AspNet.WebApi.Owin Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.2.6

如果需要静态文件支持需安装 Microsoft.Owin.StaticFiles Install-Package Microsoft.Owin.StaticFiles -Version 4.0.0

如果Microsoft.Owin.Host.HttpListener不在启动项目里面,需要手动安装到启动项目里面 Install-Package Microsoft.Owin.Host.HttpListener -Version 4.0.0

在App.Config中添加如下代码(如果没有的话)

添加Startup.cs

public class Startup { public void Configuration(IAppBuilder app) { app.UseErrorPage(); var config = GetWebApiConfig(); app.UseWebApi(config); app.UseFileServer(new FileServerOptions() { RequestPath = PathString.Empty, FileSystem = new PhysicalFileSystem(@".\www") }); app.UseWelcomePage(""); } private HttpConfiguration GetWebApiConfig() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "Default", routeTemplate: "api/{controller}/{action}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "Request", routeTemplate: "api/crawler/request", defaults: new { controller = "CrawlerApi", action = "request" } ); config.Routes.MapHttpRoute( name: "ServerInfo", routeTemplate: "api/crawler/server/info", defaults: new { controller = "CrawlerApi", action = "serverinfo" } ); //config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json")); config.Formatters.Remove(config.Formatters.XmlFormatter); config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }; return config; } }

在启动项目下添加www文件夹 并在其中建立 index.html 内容随意

将index.html 复制到输出目录属性设置为 如果较新则复制

调用如下代码启动Owin网站

WebApp.Start(baseUrl);


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3